<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;

class Base extends Controller
{
    public $adminuser;

    /**
     * 如果用户没有登录则跳转到登录页面
     */
    public function _initialize()
    {
        $isLogin = $this->isLogin();
        if (!$isLogin) {
            return $this->redirect('Login/index');
        }
    }

    /**
     * 检测用户是否登录
     * @return bool
     */
    public function isLogin()
    {
        $user = $this->getUserInfo();
        if ($user) {
            return true;
        }
        return false;
    }

    /**
     * 获取用户信息
     * @return mixed
     */
    public function getUserinfo()
    {
        if (!$this->adminuser) {
            $adminUser = session('se_name');
        }
        return $adminUser;
    }

    /**
     * 权限判断
     * Base constructor.
     */
    public function __construct(Request $request)
    {
        parent::__construct();
        //获取当前控制器以及方法名称
        $request         = Request::instance();
        $controller_name = $request->controller();
        $action          = $request->action();

        //绕过Index和登出页
        if ($controller_name == 'Index') {
            return true;
        }
        if ($controller_name == 'Login' && $action == 'logout') {
            return true;
        }

//        //判断是否有权限进行操作
        if (session('url') != '*' && !in_array($controller_name . '/' . $action,
                session('url'))
        ) {
            return $this->error('没有权限访问该功能！', url('Index/welcome'));
        }
    }

}